package cz.mar0127.sokoban;


import cz.mar0127.sokoban.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.Path.Direction;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;

public class GraphicsView extends View {

        @Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		// TODO Auto-generated method stub
		super.onSizeChanged(w, h, oldw, oldh);
		this.display = w / 10;
	}

		private int heroX = 6;
        private int heroY = 4;

        private int lx = 10;
        private int ly = 10;
        
        private int display;
	
	//TODO
	//nacitat levely ze souboru

        private int level[] = {1,1,1,1,1,1,1,1,1,0,
                               1,0,0,0,0,0,0,0,1,0,
                               1,0,2,3,3,2,1,0,1,0,
                               1,0,1,3,2,3,2,0,1,0,
                               1,0,2,3,3,2,4,0,1,0,
                               1,0,1,3,2,3,2,0,1,0,
                               1,0,2,3,3,2,1,0,1,0,
                               1,0,0,0,0,0,0,0,1,0,
                               1,1,1,1,1,1,1,1,1,0,
                               0,0,0,0,0,0,0,0,0,0
        };

        Bitmap[] bmp;
        Bitmap bBox, bBoxOK, bWall, bHero, bSpace, bGoal;

        public GraphicsView(Context context) {
                super(context);
                setFocusable(true);
                setFocusableInTouchMode(true);
                
                this.display = 0;

                bmp = new Bitmap[6];

                bmp[0] = BitmapFactory.decodeResource(getResources(), R.drawable.empty);
                bmp[1] = BitmapFactory.decodeResource(getResources(), R.drawable.wall);
                bmp[2] = BitmapFactory.decodeResource(getResources(), R.drawable.box);
                bmp[3] = BitmapFactory.decodeResource(getResources(), R.drawable.goal);
                bmp[4] = BitmapFactory.decodeResource(getResources(), R.drawable.hero);
                bmp[5] = BitmapFactory.decodeResource(getResources(), R.drawable.boxok);

        }

        public boolean onKeyDown(int keyCode, KeyEvent event) {
                Log.d("MOJE","keyCode =" + keyCode + ", event = " + event);

		// TODO
		// osetrit v oddelenych metodach testujicich pohyb

                if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN)
                {
          
                }

                if (keyCode == KeyEvent.KEYCODE_DPAD_UP)
                {
           
                }

                if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT)
                {
                
                }
       
                if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT)
                {
                
                }

		//TODO
		//toto neni optimalni varianta prekreslovani

                Rect selRect = new Rect();
                selRect.left = (heroX-2)*32;
                selRect.top = (heroY-2)*32;
                selRect.right = (heroX+2)*32;
                selRect.bottom = (heroY+2)*32;

                invalidate(selRect);
                return true;

        }

        @Override
        protected void onDraw(Canvas canvas) {

                Paint tPaint = new Paint();

                for (int i = 0; i < 10; i++)
                        for (int j = 0; j < 10; j++) {
                        // TODO 
                        // pozor na rozliseni telefonu, toto vykreslovani s nim nepocita 
                                canvas.drawBitmap(bmp[level[i*10+j]], j*display, i*display, tPaint);

                        }
        }


 }